﻿using ScannerWatcher.Models;
using ScannerWatcher.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScannerWatcher
{
    public partial class FrmCaseTree : Form
    {
        public delegate void SelectCategoryEventHandler(case_question_category category, FileItem fileItem);

        public event SelectCategoryEventHandler OnSelectCategory;
        public event SelectCategoryEventHandler OnUnSelectCategory;
        private case_question_category undefinedCategory = null;

        public FileItem FileItem { get; set; }
        public FrmCaseTree()
        {
            InitializeComponent();
        }

        public void LoadCategory(string caseId)
        {
            tvwCategory.Nodes.Clear();

            LoadCategory(tvwCategory.Nodes, caseId, "-1");

            tvwCategory.ExpandAll();
        }

        private void LoadCategory(System.Windows.Forms.TreeNodeCollection nodes, string caseId, string parentId)
        {
            var result = CaseHelper.GetCaseQuestionCategory(caseId, parentId);

            if (result.success)
            {
                foreach (var cat in result.data)
                {
                    if (cat.type != "2")
                    {
                        System.Windows.Forms.TreeNode node = new System.Windows.Forms.TreeNode();
                        node.Text = cat.name;
                        node.Tag = cat;
                        node.ImageIndex = GetNodeIcon(cat.type);
                        node.SelectedImageIndex = 0;

                        nodes.Add(node);

                        LoadCategory(node.Nodes, caseId, cat.id);
                    }

                    if (cat.name == "未归类")
                    {
                        undefinedCategory = cat;
                    }
                }
            }
        }

        private int GetNodeIcon(string type)
        {
            if (type == "0")
            {
                return 3;
            }
            else if (type == "1")
            {
                return 4;
            }
            else if (type == "2")
            {
                return 5;
            }
            else if (type == "3")
            {
                return 2;
            }
            else
            {
                return 1;
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (tvwCategory.SelectedNode != null)
            {
                var category = (case_question_category)tvwCategory.SelectedNode.Tag;

                if (category.allow_create_file)
                {
                    OnSelectCategory?.Invoke(category, FileItem);
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("若要归类一个案件材料，请您选择一个具体的案件事实。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请选择一个归类节点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();

            OnUnSelectCategory?.Invoke(null, FileItem);
        }
    }
}
